<?php
namespace app\util;

use Medoo\Medoo;

/**
 * Mysql
 * @author: JiaMeng <666@majiameng.com>
 * Trait Mysql
 * @method null select(string $table, array $join, array|null $columns, array|null $where, callable|null $callback)
 * @method mixed get(string $table, array|string $columns, array $where)
 * @method bool has(string $table, array $where)
 * @method mixed rand(string $table, array|string $column, array $where)
 * @method int count(string $table, array $where)
 * @method string max(string $table, string $column, array|null $where)
 * @method string min(string $table, string $column, array|null $where)
 * @method string avg(string $table, string $column, array|null $where)
 * @method string sum(string $table, string $column, array|null $where)
 */
class Mysql
{
    private static $instance = null;
    private $config;
    private $client;

    private function __construct()
    {
        $this->config = DATABASE_CONFIG['mysql'];
        $this->client = new Medoo([
            'database_type' => $this->config['type'],
            'database_name' => $this->config['database'],
            'server'        => $this->config['hostname'],
            'username'      => $this->config['username'],
            'password'      => $this->config['password'],
            'charset'       => $this->config['charset'],
            'port'          => $this->config['hostport'],
            'prefix'        => $this->config['prefix'],
        ]);
    }


    public static function init()
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Description:  __call
     * @author: JiaMeng <666@majiameng.com>
     * Updater:
     * @param $gateway
     * @param $config
     * @return mixed
     */
    public function __call($gateway, $config)
    {
        return $this->client->$gateway(...$config);
    }
}
